package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 北方网解析
 * @author Persh
 *
 */
public class EnorthNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.enorth.com.cn/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml"};


	
	public boolean isDetailPage(String url) {
		if(url.startsWith("http://ms.enorth.com.cn")){
			return false;
		}
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
			title =doc.select("div.title").text();
			if(title==null || title.trim().length()==0){
				title =doc.select("div#setInfo>div.nph_set_title>h1").text();
				if(title==null || title.trim().length()==0){
					title =doc.select("td.zi24").text();
					if(title==null || title.trim().length()==0){
						title =doc.select("title").text();
						title=StringUtils.substringBefore(title, "-");
						}
				}
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("div#Content"));
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url,doc.select("div#modePhoto.nph_photo>div.nph_cnt>div>p"));
				if(content==null || content.trim().length()==0){
					content=HtmlCleaner.getContentHtml(url,doc.select("td.hanggao24"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div.xw"));
						if(StringUtils.isBlank(content)){
							content=HtmlCleaner.getContentHtml(url,doc.select("p.sc"));
						}
					}
				}
			}
			if(StringUtils.isBlank(content)){
				content=doc.select(".content").html();
			}
			
			date=DateUtils.matchDate(doc.select("span#pubtime_baidu").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div#Time").text());
			}
			
			if(date==null){
				date=DateUtils.matchDate(doc.select(".info span:eq(3)").text());
			}
			
			String author=JSoupUtils.matchAuthor(doc, "稿源：","來源：");
			news.setAuthor(author);
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		EnorthNewsAnalyse analyse=new EnorthNewsAnalyse();
		String url="http://economy.enorth.com.cn/system/2014/07/02/011986877.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.isDetailPage(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
